package com.cal.utms.dao.payment;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.PlacementSheet;
import com.cal.utms.logAudit.AuditLogInterceptor;
import com.cal.utms.util.HibernateUtil;

public class PlacementSheetDaoImpl implements PlacementSheetDao {

	@Override
	public Object savePlacementSheet(PlacementSheet placementSheet) {
		Object obj = new Integer(-1);
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.save(placementSheet);
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return obj;

	}

	@Override
	public PlacementSheet getPlacementSheetByFundDate(int fundid, Date date) {
		PlacementSheet placementSheet = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			placementSheet = (PlacementSheet) session.createQuery("from PlacementSheet P where P.date=:date and P.fund.id=:fundId").setParameter("date", date)
					.setParameter("fundId", fundid).uniqueResult();
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}
		} finally {
			if (session != null) {
				// session.close();
			}
		}
		return placementSheet;

	}

	public Object updatePlacementSheet(PlacementSheet sheet) {
		Object obj = new Integer(-1);
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.createQuery("delete from PlacementSheetRecord P where P.placementSheet.id=:id").setParameter("id", sheet.getId()).executeUpdate();
			obj = session.merge(sheet);
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return obj;
	}

	public PlacementSheet getPlacementSheetById(int plasementsheetid) {
		PlacementSheet placementSheet = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			placementSheet = (PlacementSheet) session.get(PlacementSheet.class, plasementsheetid);
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}

		} finally {
			if (session != null) {
				// session.close();
			}
		}
		return placementSheet;
	}

	public Object confirm(PlacementSheet sheet) {
		Object obj = new Integer(-1);
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.merge(sheet);
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return obj;
	}

}
